package ru.averta.notes.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.averta.notes.domain.abstr.AbstractPersistentObject;
import ru.averta.notes.exceptions.UnsupportedPersistentObject;

import java.io.Serializable;

/**
 *
 * @author fedor.belov
 */
public class FakeGenerator implements IdentifierGenerator  {

    private final Logger logger = LoggerFactory.getLogger(FakeGenerator.class);

    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        logger.trace("Генерируем id-заглушку для объекта {}", object);
        if (object instanceof AbstractPersistentObject) {
            AbstractPersistentObject bObject = (AbstractPersistentObject) object;
            /*
            if (bObject.getId() == 0) {
                return bObject.getFakeId();
            } else { 
                return bObject.getId();
            }
             *
             */

            return bObject.getId();
        }
        
        throw new UnsupportedPersistentObject(object);
    }

}
